package com.zbkj.admin.task.activity;

import com.zbkj.admin.service.SeckillService;
import com.zbkj.admin.task.order.OrderReceiptTask;
import com.zbkj.common.model.activity.SfullDiscountActivity;
import com.zbkj.common.model.activity.SportfolioMarketActivity;
import com.zbkj.common.request.merchant.MerchantSfullDiscountSearchRequest;
import com.zbkj.common.request.merchant.MerchantSportfolioMarketSearchRequest;
import com.zbkj.common.utils.CrmebDateUtil;
import com.zbkj.service.service.MerchantSportfolioMarketActivityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 满赠组合活动状态检测
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Component("SportfolioMarketTask")
public class SportfolioMarketTask {

    //日志
    private static final Logger logger = LoggerFactory.getLogger(OrderReceiptTask.class);

    @Autowired
    private MerchantSportfolioMarketActivityService activityService;


    /**
     * 一个小时检测一下活动状态
     */
    public void updateSportfolioMarketStatus() {
        // cron : 0 0 */1 * * ?
        logger.info("---SportfolioMarketTask task------满赠组合活动状态检测 - {}", CrmebDateUtil.nowDateTime());
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            List<SportfolioMarketActivity> activityList = activityService.findAllist(new MerchantSportfolioMarketSearchRequest());
            for (SportfolioMarketActivity activity : activityList) {
                String dateString = simpleDateFormat.format(new Date());
                if (dateString.compareTo(activity.getEndDate())>0 && !activity.getStatus().equals(2)){
                    //说明活动已过期, 修改状态
                    activity.setStatus(2);
                    activityService.updateSportfolioMarket(activity);
                }else if (dateString.compareTo(activity.getStartDate())<0 && !activity.getStatus().equals(0)){
                    //说明活动还没开始, 修改状态
                    activity.setStatus(0);
                    activityService.updateSportfolioMarket(activity);
                }else if (dateString.compareTo(activity.getStartDate())>=0 && dateString.compareTo(activity.getEndDate())<0 && !activity.getStatus().equals(1)){
                    //说明活动正在进行中, 修改状态
                    activity.setStatus(1);
                    activityService.updateSportfolioMarket(activity);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("SportfolioMarketTask.task" + " | msg : " + e.getMessage());
        }
    }

}
